package com.sky.mapper;

import com.sky.entity.OrderDetail;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderDetailMapper {
    /**
     * 批量插入订单详情
     * @param orderDetails
     */
    void insertBatch(List<OrderDetail> orderDetails);

    /**
     * 根据订单id查询订单详情
     * @param orderId
     * @return
     */
    @Select("select * from order_detail where order_id = #{orderId}")
    List<OrderDetail> getByOrderId(Long orderId);

    /**
     * 根据时间范围查询top10
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select("select name,count(name) as number from order_detail where order_detail.order_id in" +
            " (select id from orders where order_time between #{beginTime} and #{endTime}) " +
            "group by name order by count(name) desc limit 10")
    List<Map<String,Object>> top10(LocalDateTime beginTime, LocalDateTime endTime);
}
